package edu.jmu.restful;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class RestfulUserController {
	@Autowired
	private Counter counter ;
	@Autowired
	private UserDao userDao;
	
	@RequestMapping(value="index")
	public  String index(){
		
		System.out.println("index");
		System.out.println(counter+"="+counter.incrementAndGet());
		System.out.println(userDao);
		return "users" ;
	}
	
	@RequestMapping(value="/users",method=RequestMethod.GET)
	public String allUser(Model model){
		System.out.println("GET:get all users");
		List<User> userList = userDao.findAllUser();
		for (User user : userList) {
			System.out.println(user);
		}
		model.addAttribute(userList);
		return "users";
	}
	
	@RequestMapping(value="/users/{id}",method=RequestMethod.GET)
	public String getUserById(@PathVariable Integer id, Model model){
		System.out.println("GET:get a user by id="+id);
		User user = userDao.findUserById(id);
		model.addAttribute(user);
		return "user";
	}
	
	@RequestMapping(value="/users/{id}/profile",method=RequestMethod.GET)
	public String modifyUser(@PathVariable Integer id, Model model){
		System.out.println("GET:get a user by id="+id);
		User user = userDao.findUserById(id);
		model.addAttribute(user);
		return "userModify";
	}
	
	//这里如果使用RequestMethod.PUT，虽然可以执行到，但无法将表单内容映射到user对象上
	@RequestMapping(value="/users/{id}",method=RequestMethod.POST)
	public @ResponseBody String modify(User user){
		
		System.out.println("put:modify a user by id");
		System.out.println(user);
		int flag = userDao.updateUser(user);
		return flag==-1?"{\"success\":false}":"{\"success\":true}";
	}
	
	/*@RequestMapping(value="/users/{id}",method=RequestMethod.PUT)
	public String modify(User user){
		System.out.println("PUT:modify a user by id");
		System.out.println(user);
		int flag = userDao.updateUser(user);
		return "redirect:/users/"+user.getUserid()+"/profile";
	}*/
	
	
	@RequestMapping(value="/users/{id}",method=RequestMethod.DELETE)
	public @ResponseBody String deleteUserById(@PathVariable Integer id){
		System.out.println("DELETE:delete a user by id");
		int flag = userDao.deleteUserById(id);
		return flag==-1?"{\"success\":false}":"{\"success\":true}";
	}
	
	@RequestMapping(value="/users",method=RequestMethod.POST)
	public String addUser(User user) {
		System.out.println("POST:create a new user");
		user.setUserid(Counter.getId());
		int flag = userDao.addUser(user);
		return "redirect:users";
		/*if(flag==1){
			return "创建用户成功，用户信息为:"+user;
		}else{
			return "创建用户失败";
		}*/
	}
	
	
	
	

}
